//Copyright 2008 by Wes Corp for Trafficman  All rights reserved.abstract 
package org.trafficman.trafficman_main;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.io.FileNotFoundException;

public class TrafficmanFilesNotesDbAdapterEdit {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_TM_TITLE = "jv_title";
    public static final String KEY_TM_SELECTED_FILE_ID = "jv_selected_file_id";
    public static final String KEY_TM_DATETIME_CREATED = "datetime_record_created";
    public static final String KEY_TM_DATETIME_UPDATED = "datetime_record_updated";
    private SQLiteDatabase mDb;
    private final Context mCtx;
    private String str_Current_Table;

    public TrafficmanFilesNotesDbAdapterEdit(Context ctx) {
        this.mCtx = ctx;
    }

    public TrafficmanFilesNotesDbAdapterEdit open() throws SQLException {

        if (TrafficmanConstants.ACTIVE_DEVICE.equals("sdcard")) {

            mDb = SQLiteDatabase.open(TrafficmanConstants.DATABASE_FILES_SDCARD, null);
            if (mDb == null) {
                mDb = SQLiteDatabase.create(TrafficmanConstants.DATABASE_FILES_SDCARD,
                        TrafficmanConstants.DATABASE_VERSION, null);
                mDb.execSQL("create table if not exists jv_files_sdcard" + TrafficmanConstants.DV_VER + " (_id integer primary key autoincrement, jv_title text not null, jv_selected_file_id integer, datetime_record_created text not null, datetime_record_updated text not null);");
            }
            str_Current_Table = TrafficmanConstants.DATABASE_TABLE_JV_FILES_SDCARD;

        } else {
            try {
                mDb = mCtx.openDatabase(TrafficmanConstants.DATABASE_FILES, null);
                str_Current_Table = TrafficmanConstants.DATABASE_TABLE_JV_FILES;
            } catch (FileNotFoundException e) {
                try {
                    mDb = mCtx.createDatabase(TrafficmanConstants.DATABASE_FILES,
                            TrafficmanConstants.DATABASE_VERSION, 0, null);
                // mDb.execSQL(DATABASE_CREATE);
                } catch (FileNotFoundException e1) {
                    throw new SQLException("Could not create database");
                }
            }
        }
        return this;
    }

    public void close() {
        mDb.close();
    }

    public long createNote(String jv_title, int jv_selected_file_id,
            String datetime_record_created, String datetime_record_updated) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TM_TITLE, jv_title);
        initialValues.put(KEY_TM_SELECTED_FILE_ID, jv_selected_file_id);
        initialValues.put(KEY_TM_DATETIME_CREATED, datetime_record_created);
        initialValues.put(KEY_TM_DATETIME_UPDATED, datetime_record_updated);
        return mDb.insert(str_Current_Table, null,
                initialValues);
    }

    public boolean deleteNote(long rowId) {
        return mDb.delete(str_Current_Table, KEY_ROWID + "=" + rowId, null) > 0;
    }

    public Cursor fetchAllNotes() {
        return mDb.query(str_Current_Table, new String[]{
                    KEY_ROWID, KEY_TM_TITLE, KEY_TM_SELECTED_FILE_ID,
                    KEY_TM_DATETIME_CREATED, KEY_TM_DATETIME_UPDATED
                }, null, null,
                null, null, null);
    }

    public Cursor fetchNote(long rowId) throws SQLException {
        Cursor result = mDb.query(true, str_Current_Table,
                new String[]{KEY_ROWID, KEY_TM_TITLE,
                    KEY_TM_SELECTED_FILE_ID, KEY_TM_DATETIME_CREATED,
                    KEY_TM_DATETIME_UPDATED
                }, KEY_ROWID + "=" + rowId,
                null, null, null, null);
        if ((result.count() == 0) || !result.first()) {
            // throw new SQLException("No note matching ID: " + rowId);
        }
        return result;
    }

    public boolean updateNote(long rowId, String title,
            int jv_selected_file_id, String datetime_record_created,
            String datetime_record_updated) {
        ContentValues args = new ContentValues();
        args.put(KEY_TM_TITLE, title);
        args.put(KEY_TM_SELECTED_FILE_ID, jv_selected_file_id);
        args.put(KEY_TM_DATETIME_CREATED, datetime_record_created);
        args.put(KEY_TM_DATETIME_UPDATED, datetime_record_updated);
        return mDb.update(str_Current_Table, args, KEY_ROWID + "=" + rowId, null) > 0;
    }

    public Cursor select_JV_Current_File(long rowId) {
        // TODO Auto-generated method stub
        // set all rows to not selected
        // set row we want to selected.

        return null;
    }
}
